package com.ls.lsqc.modules.sys.service;

import java.text.NumberFormat;
import java.text.ParseException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lsqc.common.service.CrudService;
import com.ls.lsqc.common.utils.DateUtils;
import com.ls.lsqc.modules.sys.dao.LogDao;
import com.ls.lsqc.modules.sys.dao.SequenceDao;
import com.ls.lsqc.modules.sys.entity.Log;
import com.ls.lsqc.modules.sys.entity.Sequence;

/**
 * 序列Service
 * @author LIUPENG
 * @version 2016-10-28
 */
@Service
@Transactional(readOnly = true)
public class SequenceService extends CrudService<LogDao, Log> {

    
    @Autowired
    private SequenceDao sequenceDao;
    @Transactional(readOnly = false)
    public Sequence  getSequence(String code){
        
        Sequence  sequence=new Sequence();
        
        sequence.setCode(code);
        
        sequenceDao.update(sequence);
        
        sequence=sequenceDao.getByCode(code);
        
        return sequence;
    }
    @Transactional(readOnly = false)
    public void  updateNumber(String code){
        
        sequenceDao.updateNumber(code);
        
    }
    @Transactional(readOnly = false)
    public void  insertSequence(String code){
        
        sequenceDao.insertSequence(code);
        
    }
    
    //
    public Sequence  getByCode(String code){
        
        return sequenceDao.getByCode(code);
    }
    
    // 通过制作方法获取生产期号
    public Sequence  getKeyWord(String makeType){
        
        return sequenceDao.getKeyWord(makeType);
    }
    
    /**
     * 内部款号Excel用
     * @return
     * @throws ParseException 
     */
    @Transactional(readOnly = false)
    public Sequence styleNoCloud(String customName,String makeType) throws ParseException{
    	String customStr = customName+"."+makeType.substring(makeType.length()-1, makeType.length());
    	Sequence sequenceCheck = sequenceDao.getCodeInfo(customStr);
    	Sequence sequenceType = sequenceDao.getKeyWord(makeType);
    	customStr = customStr+"."+sequenceType.getKeyWord();
        if (sequenceCheck==null) {
        	sequenceDao.insertSequence(customStr);
		}else{
			if (!customStr.equals(sequenceCheck.getCode())) {
				sequenceDao.updateSequence(sequenceCheck.getCode(),customStr);
			}
		}
        Sequence sequence = getSequence(customStr);
        //连翻
        NumberFormat formatter = NumberFormat.getNumberInstance();
        formatter.setMinimumIntegerDigits(4);
        formatter.setGroupingUsed(false);
        String s = formatter.format(sequence.getNumber());
        if ("9999".equals(s)) {
        	sequenceDao.updateNumber(customStr);
		}
        sequence.setReturnCode(customStr+"-"+s);
        return sequence;
    }
    
    /**
     * 财务业务结算号
     * @return
     * @throws ParseException 
     */
    @Transactional(readOnly = false)
    public Sequence financeCode() throws ParseException{
    	Sequence sequenceCheck = sequenceDao.getCodeInfo(DateUtils.getDate("yyyy-MM"));
        if (sequenceCheck==null) {
        	sequenceDao.insertSequence(DateUtils.getDate("yyyy-MM"));
		}
        Sequence sequence = getSequence(DateUtils.getDate("yyyy-MM"));
        //连翻
        NumberFormat formatter = NumberFormat.getNumberInstance();
        formatter.setMinimumIntegerDigits(4);
        formatter.setGroupingUsed(false);
        String s = formatter.format(sequence.getNumber());
        sequence.setReturnCode("FB." + DateUtils.getDate("yyyyMM") + s);
        return sequence;
    }
    
}
